package com.scs.application.modules.svn.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.svn.entity.SvnVersion;
import com.scs.application.modules.svn.mapper.SvnVersionMapper;
import com.scs.application.modules.svn.service.SvnVersionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * svn版本记录表 服务实现类
 * </p>
 */
@Service
public class SvnVersionServiceImpl extends BaseServiceImpl<SvnVersionMapper, SvnVersion> implements SvnVersionService {

    //更新dev-sit sit-release是否合并
    @Override
    public void updateMergeInfo() {
        List<SvnVersion> svnVersionDevList = this.list(Wrappers.<SvnVersion>query().eq("version_lib","dev").ne("ifnull(flag_merge,0)", 1));
        List<SvnVersion> svnVersionSitList = this.list(Wrappers.<SvnVersion>query().eq("version_lib","sit").ne("ifnull(flag_merge,0)",1));
        List<SvnVersion> svnVersionSitAllList = this.list(Wrappers.<SvnVersion>query().eq("version_lib","sit"));
        List<SvnVersion> svnVersionReleaseList = this.list(Wrappers.<SvnVersion>query().eq("version_lib","release"));
        List<String> versionLibAll = Arrays.asList("dev","sit","release");
        List<SvnVersion> svnVersionFindList;

        List<SvnVersion> svnVersionUpdateList = new ArrayList<SvnVersion>();

        //dev-sit
        for(SvnVersion svnVersion : svnVersionDevList){
            svnVersionFindList=null;
            if (svnVersionSitList != null && svnVersionSitList.size() > 0) {
                svnVersionFindList = svnVersionSitAllList.stream().filter(
                        svnVersionFind -> svnVersionFind.getSubmitRemark().indexOf(svnVersion.getVersionNum()) != -1
                ).collect(Collectors.toList());
            }

            if (svnVersionFindList != null && svnVersionFindList.size() > 0){ //找到被合并版本
                svnVersion.setFlagMerge(true).setMergeVersions(
                        StringUtils.join(svnVersionFindList.stream().map(SvnVersion::getVersionNum).collect(Collectors.toList()), ",")
                );
            }else { //未找到
                svnVersion.setFlagMerge(false);
            }
            svnVersionUpdateList.add(svnVersion);
        }

        //sit-release
        for(SvnVersion svnVersion : svnVersionSitList){
            svnVersionFindList=null;
            if (svnVersionReleaseList != null && svnVersionReleaseList.size() > 0) {
                svnVersionFindList = svnVersionReleaseList.stream().filter(
                        svnVersionFind -> svnVersionFind.getSubmitRemark().indexOf(svnVersion.getVersionNum()) != -1
                ).collect(Collectors.toList());
            }

            if (svnVersionFindList != null && svnVersionFindList.size() > 0){ //找到被合并版本
                svnVersion.setFlagMerge(true).setMergeVersions(
                        StringUtils.join(svnVersionFindList.stream().map(SvnVersion::getVersionNum).collect(Collectors.toList()), ",")
                );
            }else { //未找到
                svnVersion.setFlagMerge(false);
            }
            svnVersionUpdateList.add(svnVersion);
        }

        if (svnVersionUpdateList.size() > 0) this.updateBatchById(svnVersionUpdateList);

        //是否包含 合并版本
        svnVersionUpdateList = new ArrayList<SvnVersion>();
        List<SvnVersion> svnVersionList = this.list(Wrappers.<SvnVersion>query().ne("ifnull(flag_have_version,0)",1));
        for(SvnVersion svnVersion : svnVersionList){
            if (svnVersion.getSubmitRemark().indexOf("合并版本") == -1) {
                svnVersion.setFlagHaveVersion(false);
            }else {
                svnVersion.setFlagHaveVersion(true);
            }
            svnVersionUpdateList.add(svnVersion);
        }
        if (svnVersionUpdateList.size() > 0) this.updateBatchById(svnVersionUpdateList);
    }
}
